/**
 * @name iYiu 2018-12-29
 * 
 */
! function () {
    var u = navigator.userAgent;
    var weixin = u.indexOf('MicroMessenger') > -1 ? true : false; //微信
    var ifChrome = u.match(/Chrome/i) != null && u.match(/Version\/\d+\.\d+(\.\d+)?\sChrome\//i) ==
        null ? true : false;
    var ifAndroid = (u.match(/(Android);?[\s\/]+([\d.]+)?/)) ? true : false;
    // 安卓版本号
    var androidVersion = u.match(/Android\s*(\d+)/)
    androidVersion = androidVersion ? (androidVersion[1] || 0) : 0;
    var ifiPad = (u.match(/(iPad).*OS\s([\d_]+)/)) ? true : false;
    var ifiPhone = (!ifiPad && u.match(/(iPhone\sOS)\s([\d_]+)/)) ? true : false;
    var ifSafari = (ifiPhone || ifiPad) && u.match(/Safari/);
    var version = 0;
    ifSafari && (version = u.match(/Version\/([\d\.]+)/));
    version = parseFloat(version[1], 10);
    const initialTime = Date.now();
    let intHandle = null;

    function openApp(option, isAutoLaunchApp) { //安卓还在招募中，暂时只放下载
        let downloadUrl = option.downloadUrl,
            iosLink = option.iosLink;
        if (ifAndroid) {
            if (!weixin) {
                window.location.href = option.downloadUrl;
            }
            if (isAutoLaunchApp && androidVersion >= 5) {
                // 延后50毫秒
                setTimeout(function () {
                    window.location.href = option.downloadUrl;
                }, 50)
            }
        } else if (ifiPhone) {
            if (ifSafari && version >= 9) { // 判断safari版本 如果大于9

                document.location.href = iosLink;
                if (!ifSafari && isAutoLaunchApp) {
                    setTimeout(function () {
                        window.location.href = downloadUrl;
                    }, 100)
                }
                // setTimeout(function () {
                //     document.location.reload();
                // }, 1000)
                // 如果是自动跳转 则直接返回
                if (isAutoLaunchApp) return
            } else if (ifSafari && version < 9) {
                window.location.href = iosLink;
            }
        }
        //使用计算时差的方案打开APP
        var checkOpen = function (cb) {
            var _clickTime = +(new Date());
            function check(elsTime) {
                var hidden, visibilityChange;
                if (typeof document.hidden !== "undefined") {
                    hidden = "hidden";
                    visibilityChange = "visibilitychange";
                } else if (typeof document.mozHidden !== "undefined") {
                    hidden = "mozHidden";
                    visibilityChange = "mozvisibilitychange";
                } else if (typeof document.msHidden !== "undefined") {
                    hidden = "msHidden";
                    visibilityChange = "msvisibilitychange";
                } else if (typeof document.webkitHidden !== "undefined") {
                    hidden = "webkitHidden";
                    visibilityChange = "webkitvisibilitychange";
                }

                // 添加监听器
                document.addEventListener(visibilityChange, function () {
                    // alert("当前页面是否被隐藏：" + document[hidden]);
                    alert(elsTime)
                    if (elsTime > 3000 || document[hidden]) {
                        cb(1);
                    }
                });
                if(!document.hidden||elsTime < 3000){
                    cb(0)
                }
            }
            //启动间隔20ms运行的定时器，并检测累计消耗时间是否超过3000ms，超过则结束
            var _count = 0;
            intHandle = setInterval(function () {
                _count++;
                var elsTime = +(new Date()) - _clickTime;
                if (_count >= 100 || elsTime > 3000) {
                    clearInterval(intHandle);
                    check(elsTime);
                }
            }, 20);
        }
        checkOpen(function (opened) {
            // APP没有打开成功  并且开启自动跳转到下载页
            if (opened === 0 && option.autoRedirectToDownloadUrl) {
                location.href = downloadUrl;
            }
        });
    }

    function init(option) {
        // 如果开启自动打开
        if (option.autoLaunchApp) {
            // 打开APP
            openApp(option, true)
        }
        openApp(option, false)
    }

    rouseAPP = function (option) {
        init(option)
    }
}();